<template>
  <div class="dialog">
    <el-dialog :title="'批次' + titleText" :visible.sync="showDialog" width="55%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="品种名称:" prop="varietyId" class="w50p">
          <el-select filterable clearable v-model="formData.varietyId" filterable clearable allow-create>
            <el-option v-for="item of FishVarietyData" :key="item.fishVarietyId + 'variety'" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注:" class="w50p">
          <el-input v-model="formData.remark" clearable />
        </el-form-item>
        <el-form-item class="imgBox" label="上传图片:" prop="pictureId">
          <uploadimage ref="UploadImage" class="imgBox" @child-event="getimageurl" />
          <img v-show="showImg" :src="baseImg" alt="">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import uploadimage from "@/components/uploadimage/uploadimage.vue";
import demoApi from "@/api/document";
import DocumentApi from "@/api/document";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import homeBaseApi from "@/api/basic-info-manage/homeBase";
import { blobToBase64 } from "@/utils/convert";

import fishSpeciesImgApi from "@/api/traceability/basicsInfo/fishSpeciesImg";

export default {
  name: '',
  components: { uploadimage },
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      showImg: false,
      showDialog: false,
      FishVarietyData: [],
      formData: {},
      baseImg: '',
      rules: {
        varietyId: [
          { required: true, message: "请选择品种", trigger: ["change", "blur"] }
        ],
        pictureId: [
          { required: true, message: "请上传图片", trigger: ["change", "blur"] }
        ],
      }
    };
  },
  computed: {
  },
  watch: {
    show (v) {
      this.showDialog = v;
      this.queryFishVariety()
      v && (this.formData = {})
      this.baseImg = false
      this.$refs.UploadImage && this.$refs.UploadImage.clearImageUrl()
      this.$refs['ruleForm'] && this.$refs['ruleForm'].resetFields()
    },
    nowChanceStau (v) {
      if (v === 'add') {
        homeBaseApi.getBreedTypeNo().then(res => {
          this.formData = {
            staffName: sessionStorage.getItem('staffName'),
            breedTypeNo: res.data
          }
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
        this.editObject.pictureId && this.fileDownload(this.editObject.pictureId)
      }
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    //上传图片
    getimageurl (e) {
      let fd = new FormData();
      fd.append("file", e);
      demoApi.fileUpload(fd).then(res => {
        if (res.success) {
          this.formData.pictureId = res.data;
          this.showImg = false;
          this.$message.success("上传成功");
        } else this.$message.error(res.message);
      });
    },
    fileDownload (fileId) {
      fileId && DocumentApi.fileDownload({ fileId }).then(res => {
        blobToBase64(res.data).then(response => {
          this.baseImg = response;
          this.showImg = true;
        });
      })
    },
    //查询可选鱼类品种
    queryFishVariety () {
      FishVarietyApi.queryByCondition().then(res => {
        this.FishVarietyData = res.data;
      })
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            fishSpeciesImgApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            fishSpeciesImgApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .imgBox {
  width: 200px;
  height: 100px;

  .xunzeimage,
  .imgUrl {
    width: 100px;
    height: 100px;

    img,
    div {
      height: 100%;
      width: 100%;
    }

    .el-icon-close {
      color: #ffffff;
      transform: translate(15px, -15px);
    }
  }
}
</style>
